package com.googlecode.kipler.container.dl.operational;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;

import com.googlecode.kipler.container.dl.Individual;
import com.googlecode.kipler.container.graph.RelationalNode;
import com.googlecode.kipler.syntax.Name;
import com.googlecode.kipler.syntax.Role;


/**
 * 
 * @author İnanç Seylan
 *
 */
public class IndividualIterator implements Iterator<Individual> {
	private List<Iterator<RelationalNode<Individual, Role, Name>>> iterList = new ArrayList<Iterator<RelationalNode<Individual, Role, Name>>>();

	private int currentIndex;

	public IndividualIterator() {
	}

	public IndividualIterator(
			Iterator<RelationalNode<Individual, Role, Name>> iter) {
		iterList.add(iter);
	}

	private Iterator<RelationalNode<Individual, Role, Name>> getIterator() {
		Iterator<RelationalNode<Individual, Role, Name>> result = null;
		while (currentIndex < iterList.size()
				&& !iterList.get(currentIndex).hasNext()) {
			currentIndex++;
		}
		if (currentIndex < iterList.size()) {
			result = iterList.get(currentIndex);
		}
		return result;
	}

	public boolean hasNext() {
		return getIterator() != null;
	}

	public Individual next() {
		Iterator<RelationalNode<Individual, Role, Name>> iter = getIterator();
		if (iter == null) {
			throw new NoSuchElementException();
		}
		return iter.next().getValue();
	}

	public void remove() {

	}

	public void add(Iterator<RelationalNode<Individual, Role, Name>> iter) {
		iterList.add(iter);
	}

}
